//
//  RCMerchantsListDataModel.m
//  RocketProject
//
//  Created by chen jiedan on 13-5-28.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCMerchantsListDataModel.h"
#import "FrameworkRelativeDefines.h"

@implementation RCMerchant

@synthesize merchantName = _merchantName;
@synthesize merchantID   = _merchantID;
@synthesize merchantDistance = _merchantDistance;
@synthesize merchantPrice    = _merchantPrice;
@synthesize merchantType     = _merchantType;
@synthesize merchantTags     = _merchantTags;
@synthesize merchantTagsString=_merchantTagsString;
@synthesize merchantImageUrl = _merchantImageUrl;
@synthesize merchantScore    = _merchantScore;
@synthesize isCard = _isCard;
@synthesize isTicket = _isTicket;
@synthesize isBenefit = _isBenefit;

- (id) initWithDic:(NSDictionary *)data
{
    self = [super init];
    if (self) {
        self.merchantName      =  [data stringValueForKey:@"merchant_name" defaultValue:nil operation:NSStringOperationTypeNone];
        self.merchantID        = [data stringValueForKey:@"merchant_id" defaultValue:nil operation:NSStringOperationTypeNone];
        self.merchantPrice     = [data stringValueForKey:@"price" defaultValue:@"" operation:NSStringOperationTypeNone];
        self.merchantDistance  = [data stringValueForKey:@"distance" defaultValue:@"" operation:NSStringOperationTypeNone];
        self.merchantTags  = [data arrayValueForKey:@"merchant_tags" defaultValue:nil];
        if (nil == self.merchantTags) {
            self.merchantTags  = [data arrayValueForKey:@"keywords" defaultValue:nil];
        }
        
        self.merchantTagsString  = [data stringValueForKey:@"merchant_tags" defaultValue:nil operation:NSStringOperationTypeTrim];
        if (nil == self.merchantTagsString) {
            self.merchantTagsString  = [data stringValueForKey:@"keywords" defaultValue:nil operation:NSStringOperationTypeTrim];
        }
        
        self.merchantImageUrl = [data stringValueForKey:@"picurl" defaultValue:nil operation:NSStringOperationTypeTrim];
        self.merchantScore = [data intValueForKey:@"score" defaultValue:0];
        
        NSInteger card = [data intValueForKey:@"card" defaultValue:0];
        NSInteger ticket = [data intValueForKey:@"ticket" defaultValue:0];
        NSInteger benefit = [data intValueForKey:@"benefit" defaultValue:0];
        if (card == 1) {
            self.isCard = YES;
        }else{
            self.isCard = NO;
        }
        
        if (ticket == 1) {
            self.isTicket = YES;
        }else{
            self.isTicket = NO;
        }
        
        if (benefit == 1) {
            self.isBenefit = YES;
        }else{
            self.isBenefit = NO;
        }

    }
    return self;
}


- (id) initRecommendResWithDic:(NSDictionary *)data
{
    self = [super init];
    if (self) {
        self.merchantName      =  [data stringValueForKey:@"name" defaultValue:nil operation:NSStringOperationTypeNone];
        self.merchantID        = [data stringValueForKey:@"id" defaultValue:nil operation:NSStringOperationTypeNone];
        self.merchantPrice     = [data stringValueForKey:@"averagecost" defaultValue:@"" operation:NSStringOperationTypeNone];
        self.merchantDistance  = [data stringValueForKey:@"distance" defaultValue:@"" operation:NSStringOperationTypeNone];
        self.merchantTagsString = [data stringValueForKey:@"keywords" defaultValue:nil operation:NSStringOperationTypeNone];
        self.merchantImageUrl = [data stringValueForKey:@"picurl" defaultValue:nil operation:NSStringOperationTypeTrim];
        self.merchantScore = [data intValueForKey:@"score" defaultValue:0];
        
        NSInteger card = [data intValueForKey:@"card" defaultValue:0];
        NSInteger ticket = [data intValueForKey:@"ticket" defaultValue:0];
        NSInteger benefit = [data intValueForKey:@"benefit" defaultValue:0];
        if (card == 1) {
            self.isCard = YES;
        }else{
            self.isCard = NO;
        }
        
        if (ticket == 1) {
            self.isTicket = YES;
        }else{
            self.isTicket = NO;
        }
        
        if (benefit == 1) {
            self.isBenefit = YES;
        }else{
            self.isBenefit = NO;
        }

    }
    return self;
}


- (id) initMyCollectWithDic:(NSDictionary *)data
{
    self = [super init];
    if (self) {
        self.merchantName      =  [data stringValueForKey:@"title" defaultValue:nil operation:NSStringOperationTypeNone];
        self.merchantID        = [data stringValueForKey:@"shopid" defaultValue:nil operation:NSStringOperationTypeNone];
        self.merchantPrice     = [data stringValueForKey:@"price" defaultValue:@"" operation:NSStringOperationTypeNone];
//        self.merchantDistance  = [data stringValueForKey:@"distance" defaultValue:@"" operation:NSStringOperationTypeNone];
        self.merchantTagsString = [data stringValueForKey:@"keywords" defaultValue:nil operation:NSStringOperationTypeNone];
        self.merchantImageUrl = [data stringValueForKey:@"picurl" defaultValue:nil operation:NSStringOperationTypeTrim];
        self.merchantScore = [data intValueForKey:@"score" defaultValue:0];
        
        NSInteger card = [data intValueForKey:@"card" defaultValue:0];
        NSInteger ticket = [data intValueForKey:@"ticket" defaultValue:0];
        NSInteger benefit = [data intValueForKey:@"benefit" defaultValue:0];
        if (card == 1) {
            self.isCard = YES;
        }else{
            self.isCard = NO;
        }
        
        if (ticket == 1) {
            self.isTicket = YES;
        }else{
            self.isTicket = NO;
        }
        
        if (benefit == 1) {
            self.isBenefit = YES;
        }else{
            self.isBenefit = NO;
        }
        
    }
    return self;
}


-(void) dealloc
{
    KF_RELEASE_SAFELY(_merchantTags);
    KF_RELEASE_SAFELY(_merchantID);
    KF_RELEASE_SAFELY(_merchantName);
    KF_RELEASE_SAFELY(_merchantDistance);
    KF_RELEASE_SAFELY(_merchantPrice);
    KF_RELEASE_SAFELY(_merchantType);
    KF_RELEASE_SAFELY(_merchantTagsString);
    KF_RELEASE_SAFELY(_merchantImageUrl);
    [super dealloc];
}

@end

@implementation RCMerchantsListDataModel

// 将JSON数据解析为数据模型
-(id) parseDataFromJSON:(NSDictionary*)data
{
    // 输入参数判断
    if ( nil == data )
        return nil;
    
    // 分配数据模型对象内存
    RCMerchantsListDataModel *retObject = nil;
    retObject = [[RCMerchantsListDataModel alloc] init];
    if ( nil == retObject )
        return retObject;

    retObject.err = [[[ServerError alloc] initWithDic:data] autorelease];
    
    NSMutableArray *listItems = [[NSMutableArray alloc] init];

    NSDictionary *dataDic = [data dictionaryValueForKey:@"data" defaultValue:nil];
    retObject.pn     = [dataDic intValueForKey:@"pn" defaultValue:0];
    retObject.rn     = [dataDic intValueForKey:@"rn" defaultValue:0];
    retObject.total  = [dataDic intValueForKey:@"total" defaultValue:0];

    NSArray *dataItems = [dataDic arrayValueForKey:@"items" defaultValue:nil];
    for (NSDictionary *dic in dataItems) {
        if ([dic objectForKey:@"merchant_name"] != nil) {
            RCMerchant *merchant = [[RCMerchant alloc] initWithDic:dic];
            [listItems addObject:merchant];
            [merchant release];
        }else if ([dic objectForKey:@"title"] != nil){
            RCMerchant *merchant = [[RCMerchant alloc] initMyCollectWithDic:dic];
            [listItems addObject:merchant];
            [merchant release];
        }
    }
    retObject.listItems = listItems;
    [listItems release];
    
    // 返回结果数据模型
    return [retObject autorelease];
}

-(void) dealloc
{
    [super dealloc];
}
@end


@implementation RCRecommendRestaurantListDataModel

// 将JSON数据解析为数据模型
-(id) parseDataFromJSON:(NSDictionary*)data
{
    // 输入参数判断
    if ( nil == data )
        return nil;
    
    // 分配数据模型对象内存
    RCRecommendRestaurantListDataModel *retObject = nil;
    retObject = [[RCRecommendRestaurantListDataModel alloc] init];
    if ( nil == retObject )
        return retObject;
    
    retObject.err = [[[ServerError alloc] initWithDic:data] autorelease];
    
    NSMutableArray *listItems = [[NSMutableArray alloc] init];
    
    NSDictionary *dataDic = [data dictionaryValueForKey:@"data" defaultValue:nil];
    retObject.pn     = [dataDic intValueForKey:@"pn" defaultValue:0];
    retObject.rn     = [dataDic intValueForKey:@"rn" defaultValue:0];
    retObject.total  = [dataDic intValueForKey:@"total" defaultValue:0];
    
    NSArray *dataItems = [dataDic arrayValueForKey:@"list" defaultValue:nil];
    if (nil == dataItems) {
        dataItems = [dataDic arrayValueForKey:@"items" defaultValue:nil];
    }
    for (NSDictionary *dic in dataItems) {
        if ([dic objectForKey:@"merchant_name"] != nil) {
            RCMerchant *merchant = [[RCMerchant alloc] initWithDic:dic];
            [listItems addObject:merchant];
            [merchant release];
        }else if ([dic objectForKey:@"name"] != nil){
            RCMerchant *merchant = [[RCMerchant alloc] initRecommendResWithDic:dic];
            [listItems addObject:merchant];
            [merchant release];
        }
       
    }
    retObject.listItems = listItems;
    [listItems release];
    
    // 返回结果数据模型
    return [retObject autorelease];
}

-(void) dealloc
{
    [super dealloc];
}
@end
